package com.eningqu.aipen.ui.adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.eningqu.aipen.demo.R;
import com.eningqu.aipen.demo.databinding.ItemMeidcalNodeBinding;
import com.eningqu.aipen.demo.databinding.ItemZyNodeBinding;
import com.eningqu.aipen.logic.model.medical.MedicalBookBean;
import com.eningqu.aipen.logic.model.medical.MedicalNodeWithXyModelBean;
import com.eningqu.aipen.logic.model.medical.MedicalNodeWithZyModelBean;
import com.eningqu.aipen.ui.medical.ChineseMedicalDetailActivity;
import com.eningqu.aipen.ui.medical.MedicalDetailActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Package:com.eningqu.aipen.ui.adapter
 * Author:starr
 * Time:2024/2/16 15:25
 * Description:
 */
public class MedicalZyNodeAdapter extends RecyclerView.Adapter<MedicalZyNodeAdapter.MedicalZyNodeViewHolder> {

    private List<MedicalNodeWithZyModelBean.JdListDTO> list;
    private MedicalBookBean.PageDTO patient;


    public MedicalZyNodeAdapter(List<MedicalNodeWithZyModelBean.JdListDTO> list, MedicalBookBean.PageDTO patient) {
        this.list = list;
        this.patient = patient;
    }

    @NonNull
    @Override
    public MedicalZyNodeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ItemZyNodeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.item_zy_node, viewGroup, false);
        MedicalZyNodeViewHolder holder = new MedicalZyNodeViewHolder(binding.getRoot());
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MedicalZyNodeViewHolder holder, int i) {
        MedicalNodeWithZyModelBean.JdListDTO medical = list.get(i);
        ItemZyNodeBinding binding = DataBindingUtil.getBinding(holder.itemView);
        assert binding != null;
        binding.tvDate.setText(medical.zyModelEntity.emrZyDate.substring(0, 10));
        binding.tvBefore1.setText(medical.zyModelEntity.emrZyBefore);
        binding.tvChief1.setText(medical.zyModelEntity.emrZyMain);
        binding.tvCreatePeople.setText(medical.zyModelEntity.emrYsName);
        binding.tvNow1.setText(medical.zyModelEntity.emrZyNow);
        binding.tvTime.setText(medical.zyModelEntity.emrZyDate.substring(0, 10));
        binding.clItemRoot.setOnClickListener(v -> {
            //该病程的详情页面
            Bundle bundle = new Bundle();
            bundle.putSerializable("nodeBean", medical.zyModelEntity);
            bundle.putSerializable("patient", patient);
            ChineseMedicalDetailActivity.startAction(v.getContext(), bundle);
        });

        //面相
        binding.rvFaceNine.setLayoutManager(new GridLayoutManager(binding.getRoot().getContext(), 3));
        ArrayList<String> list = new ArrayList<>();
        if (medical.zyModelEntity.emrZyFaceImgs != null) {
            String[] strs = medical.zyModelEntity.emrZyFaceImgs.split(";");
            list.addAll(Arrays.asList(strs));
            GridNineAdapter adapter = new GridNineAdapter(list);
            binding.rvFaceNine.setAdapter(adapter);
            binding.tvLook.setVisibility(View.VISIBLE);
        }


        //舌象
        binding.rvTongueNine.setLayoutManager(new GridLayoutManager(binding.getRoot().getContext(), 3));
        ArrayList<String> tongueList = new ArrayList<>();
        if (medical.zyModelEntity.emrZyTongueImgs != null) {
            String[] strs = medical.zyModelEntity.emrZyTongueImgs.split(";");
            tongueList.addAll(Arrays.asList(strs));
            GridNineAdapter tongueAdapter = new GridNineAdapter(tongueList);
            binding.rvTongueNine.setAdapter(tongueAdapter);
            binding.tvTongue.setVisibility(View.VISIBLE);
        }

    }


    @Override
    public int getItemCount() {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        return list.size();
    }

    protected static class MedicalZyNodeViewHolder extends RecyclerView.ViewHolder {
        public MedicalZyNodeViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
